package com.mjf.tankgame.v4;

/**
 * 敌人的坦克
 */
public class EnemyTank extends Tank implements Runnable{

    // 定义画布宽
    private int panelWidth = 1000;
    // 定义画布高
    private int panelHeight = 750;

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
        while (true) {

            // 这里我们判断如果 shots.size() == 0, 创建一颗子弹，放入到shots集合，并启动
            if (isLive && shots.size() < 1) {
                Shot s = null;
                // 判断坦克的方向，创建对应的子弹
                switch (getDirect()) {
                    case 0:
                        s = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        s = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2: // 向下
                        s = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:// 向左
                        s = new Shot(getX(), getY() + 20, 3);
                        break;
                }
                shots.add(s);
                // 启动
                new Thread(s).start();

            }

            // 判断坦克的方向来继续移动
            switch (getDirect()) {
                case 0: // 向上
                    for (int i = 0; i < (int)(Math.random() * 7 + 23); i++) {
                        moveUp();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1: // 向右
                    for (int i = 0; i < (int)(Math.random() * 7 + 23); i++) {
                        moveRight(panelWidth);
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2: // 向下
                    for (int i = 0; i < (int)(Math.random() * 7 + 23); i++) {
                        moveDown(panelHeight);
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:// 向左
                    for (int i = 0; i < (int)(Math.random() * 7 + 23); i++) {
                        moveLeft();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }

            if (!isLive) {
                break;
            }

            // 改变坦克方向
            setDirect((int)(Math.random() * 4));

        }
    }
}
